package lsy.redis001.lsyredis001.service.impl;

import lsy.redis001.lsyredis001.service.ArticleTagService;
import lsy.redis001.lsyredis001.table.RedisTableConstant;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class ArticleTagServiceImpl implements ArticleTagService {
    @Autowired
    private RedisTemplate redisTemplate;

    // post:articeId:tags -> mysql、java
    @Override
    public void addArticleTag(Long articleId,String tag){
        Long add = redisTemplate.opsForSet().add(RedisTableConstant.getPostArticleTag(articleId), tag);
    }

    @Override
    public void deleteArticleTag(Long articleId, String tag) {
        Long remove = redisTemplate.opsForSet().remove(RedisTableConstant.getPostArticleTag(articleId), tag);
    }

    @Override
    public void deleteArticleTag(Long articleId) {
        Boolean delete = redisTemplate.delete(RedisTableConstant.getPostArticleTag(articleId));
    }

    @Override
    public List<String> getArticleTag(Long articleId) {
        Set members = redisTemplate.opsForSet().members(RedisTableConstant.getPostArticleTag(articleId));
        return Lists.newArrayList(members);
    }
}
